package lei.f_java_string_stringbuffer;

import java.util.Arrays;

/**
 * @className: Java_String
 * @author: xl
 * @date: 2025/3/1 13:39
 * @Version: 1.0
 * @description:
 */

public class Java_String {

    public static void main(String[] args) {
        String s1 = "R unoob";              // String 直接创建
        String s2 = "Runoob";              // String 直接创建
        String s3 = s1;                    // 相同引用
        String s4 = new String("Runoob");   // String 对象创建
        String s5 = new String("Runoob");   // String 对象创建


        char[] chars = s1.toCharArray();
        System.out.println(Arrays.toString(chars));


        String str1 = "Hello";
        String str2 = "World";
        String result = str1 + ", " + str2;
        System.out.println(result); // 输出: Hello, World

        // 使用 String.join() 连接字符串数组
        String[] words = {"Java", "is", "cool"};
        String sentence = String.join(" ", words);
        System.out.println(sentence); // 输出: Java is cool


        //字符串查找
        String text = "Hello, welcome to the world of Java.";
        int index = text.indexOf("Java");
        System.out.println(index); // 输出: 28

        int lastIndex = text.lastIndexOf("o");
        System.out.println(lastIndex); // 输出: 25


        String text1 = "Hello, Java!";
        String replacedText = text1.replace('a', 'A').replace("Java", "Python");
        System.out.println(replacedText); // 输出: HellA, Python!


        String text2 = "apple,banana,cherry";
        String[] fruits = text2.split(",");
        for (String fruit : fruits) {
            System.out.println(fruit);
        }


        String text3 = "Hello Java!";
        System.out.println(text3.toLowerCase()); // 输出: hello java!
        System.out.println(text3.toUpperCase()); // 输出: HELLO JAVA!

        String text4 = "   Hello Java!   ";
        System.out.println(text4.trim()); // 输出: Hello Java!


        String formattedText = String.format("The value is %d and the name is %s.", 100, "Java");
        System.out.println(formattedText); // 输出: The value is 100 and the name is Java.


        char[] chars1 = str1.toCharArray();
        for (char c : chars1) {
            System.out.println(c);
        }

        String str3 = "1,2,3,4,5,6,7,8,9,10";
        String[] str4 = str3.split(",");
        System.out.println(Arrays.toString(str4));


    }
}

